<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="栏目">
          <j-dict-select-tag v-decorator="['channel', {}]" :triggerChange="false" dictCode="cms_channel"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类别">
          <j-dict-select-tag v-decorator="['category', {}]" :triggerChange="false" dictCode="cms_content_type"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="业务类型">
          <j-dict-select-tag v-decorator="['bizType', {}]" :triggerChange="false" dictCode="cms_bizType"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="标题（链接名）">
          <a-input placeholder="请输入标题（链接名）" v-decorator="['title', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="子标题（补充说明）">
          <a-input placeholder="请输入子标题（补充说明）" v-decorator="['subtitle', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="链接值(URL)">
          <a-input placeholder="请输入链接值(URL)" v-decorator="['urlLink', {}]" />
        </a-form-item>
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="publishDate">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'publishDate', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="publishUser">
          <a-input placeholder="请输入publishUser" v-decorator="['publishUser', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="orderNo">
          <a-input-number v-decorator="[ 'orderNo', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="状态：0-草稿；1-发布；2-废弃">
          <a-input placeholder="请输入状态：0-草稿；1-发布；2-废弃" v-decorator="['status', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否新加项：0-非，1-是">
          <a-input placeholder="请输入是否新加项：0-非，1-是" v-decorator="['isNew', {}]" />
        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否隐藏">
          <!--<a-input placeholder="请输入是否隐藏：0-非，1-是" v-decorator="['isHidden', {}]" />-->
          <j-dict-select-tag v-decorator="['isHidden', {}]" :triggerChange="true" dictCode="shifei"/>
          <!--<a-select placeholder="请选择" default-value="0" v-decorator="['isHidden', {}]">
            <a-select-option value="0">
              非
            </a-select-option>
            <a-select-option value="1">
              是
            </a-select-option>
          </a-select>-->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否热点链接">
       <!--   <a-input placeholder="请输入是否热点链接：0-非，1-是" v-decorator="['isHighlighted', {}]" />-->

          <j-dict-select-tag v-decorator="['isHighlighted', {}]" :triggerChange="true" dictCode="shifei"/>
          <!--<a-select placeholder="请选择" default-value="0" v-decorator="['isHighlighted', {}]">
            <a-select-option value="0">
              非
            </a-select-option>
            <a-select-option value="1">
              是
            </a-select-option>
          </a-select>-->
        </a-form-item>
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="创建人">
          <a-input placeholder="请输入创建人" v-decorator="['createUser', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="更新人">
          <a-input placeholder="请输入更新人" v-decorator="['updateUser', {}]" />
        </a-form-item>-->

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"


  const plainOptions = ['资讯中心', '服务中心', '热门活动'];
  const options = [
    { label: '资讯中心', value: '资讯中心' },
    { label: '服务中心', value: '服务中心' },
    { label: '热门活动', value: '热门活动' },
  ];


  export default {
    name: "CmsRefLinkModal",
    data () {
      return {
        title:"操作",
        visible: false,
        plainOptions,
        options,
        value: [],
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: {
          add: "/cxm/cmsRefLink/add",
          edit: "/cxm/cmsRefLink/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      onChange(checkedValues) {
        console.log('checked = ', checkedValues);
        console.log('value = ', this.value);
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'channel','category','bizType','title','subtitle','urlLink','isHidden','isHighlighted'))
		  //时间格式化
          //this.form.setFieldsValue({publishDate:this.model.publishDate?moment(this.model.publishDate):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            //formData.publishDate = formData.publishDate?formData.publishDate.format('YYYY-MM-DD HH:mm:ss'):null;

            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>